import 'dart:async';

import 'package:distribution_mall_flutter/base/base_service.dart';
import 'package:distribution_mall_flutter/http/modules/address.dart';
import 'package:distribution_mall_flutter/pages/address/address_search/search_address_provider.dart';
import 'package:flutter/material.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'address_search_service.g.dart';

class AddressSearchService extends BaseService {
  AddressSearchService(super.ref);

  Future<void> queryAddressByName(
      String str, TextEditingController searchTextEditingController) async {
    ref.read(searchAddressIsLoadingProvider.notifier).setNewData(true);
    (await AddressApi.queryAddressByName(str)).yes((value) {
      if (searchTextEditingController.text.length >= 3) {
        if (searchTextEditingController.text == str) {
          ref.read(searchAddressProvider.notifier).setNewData(value);
          ref.read(searchAddressIsLoadingProvider.notifier).setNewData(false);
        }
      }
    });
  }
}

@riverpod
AddressSearchService addressSearchService(AddressSearchServiceRef ref) {
  return AddressSearchService(ref);
}
